<?php   // $Id: devis_lister.php 298 2008-10-12 16:17:00Z pcoustillas $ ?>
<?php
/*
Copyright (C) 2003-2004 Guy Hendrickx
Licensed under the terms of the GNU  General Public License:http://www.opensource.org/licenses/gpl-license.php
File Authors:Guy Hendrickx 
*/
require_once("include/verif.php");
require_once("include/config/common.php");
require_once("include/config/var.php");
require_once("include/language/$lang.php");
require_once("include/utils.php");
require_once("include/headers.php");
require_once("include/choix_cli.php"); ?>

<script language="javascript" src="javascripts/verifier_form.js" type="text/javascript"></script>
<script type="text/javascript">
function verif_form()
{
	if (verifier('frm_dev','black','date','vide#date_'))
	{
		document.frm_dev.submit();
	} else {
		alert("<?php aff_lang('Une des valeurs saisie n\'est pas correct.\nMerci de corriger avant de poursuivre.') ?>")	
	}

}
</script>
<script type="text/javascript" src="javascripts/confdel.js"></script>
<script type="text/javascript" src="javascripts/tri_formulaire.js"></script>
<?php

?>

<table border="0" class="page" align="center">
	<tr>
		<td class="page" align="center">
			<?php  
			require_once("include/head.php");
			?>
		</td>
	</tr>
	<tr>
		<td class="page" align="center">
		<?php
			if (isset($_GET['message']))
			{
				echo "<h1>" . $_GET['message'] . "</h1>";
			}

			if ($user_dev == "n") {
				echo "<h1>$lang_devis_droit</h1>";
				exit;
			}

			

			$sql = "SELECT resu,login, mail, num_dev, tot_htva, tot_tva, date, nom
				FROM ${tblpref}devis RIGHT JOIN ${tblpref}client on ${tblpref}devis.client_num = num_client ";

			// ----------  DEBUT DE GESTION DES FILTRES ---------------------
			if (isset($_GET['annuler_filtre'])) //vidage des filtres si demandé
			{
				$_SESSION['filtre_dev_client'] = "";
				$_SESSION['filtre_dev_num'] = "";
				$_SESSION['filtre_dev_date'] = "";
				$_SESSION['filtre_dev_HT'] = "";
				$_SESSION['filtre_dev_TTC'] = "";
				$_SESSION['filtre_dev_en_cours'] = "1";
				$_SESSION['filtre_dev_refuse'] = "";
				$_SESSION['filtre_dev_accepte'] = "";
			}

			$filtre=" WHERE num_dev > 0 AND ";

			if (isset($_GET['filtrer'])) 
			{
				// MAJ des variables de session de filtre si il y a eu demande de filtrage
				if (isset($_GET['filtre_dev_client']) && $_GET['filtre_dev_client']<>0 )
					$_SESSION['filtre_dev_client']=$_GET['filtre_dev_client'];
				if (isset($_GET['filtre_dev_client']) && $_GET['filtre_dev_client']==0 )
					unset($_SESSION['filtre_dev_client']);
				if (isset($_GET['filtre_dev_num']))
					$_SESSION['filtre_dev_num']=$_GET['filtre_dev_num'];
				if (isset($_GET['filtre_dev_date']))
					$_SESSION['filtre_dev_date']=$_GET['filtre_dev_date'];
				if (isset($_GET['filtre_dev_HT']))
					$_SESSION['filtre_dev_HT']=$_GET['filtre_dev_HT'];
				if (isset($_GET['filtre_dev_TTC']))
					$_SESSION['filtre_dev_TTC']=$_GET['filtre_dev_TTC'];
				if (isset($_GET['filtre_dev_en_cours']))
				{
					$_SESSION['filtre_dev_en_cours']=$_GET['filtre_dev_en_cours'];
				} else {
					$_SESSION['filtre_dev_en_cours']="";
				}
				if (isset($_GET['filtre_dev_refuse']))
				{
					$_SESSION['filtre_dev_refuse']=$_GET['filtre_dev_refuse'];
				} else {
					$_SESSION['filtre_dev_refuse']="";
				}
				if (isset($_GET['filtre_dev_accepte']))
				{
					$_SESSION['filtre_dev_accepte']=$_GET['filtre_dev_accepte'];
				} else {
					$_SESSION['filtre_dev_accepte']="";
				}
			}

			//Création de la requete. Ne se trouve pas dans le test "Si frm poster" car si le user revient le filtre doit tj etre en place
			if (isset($_SESSION['filtre_dev_client']) && $_SESSION['filtre_dev_client'] != "")
			{
				$filtre .= "num_client="  . $_SESSION['filtre_dev_client'] . " AND ";
			}
			if (isset($_SESSION['filtre_dev_num']) && $_SESSION['filtre_dev_num'] != "")
			{
				$filtre .= "num_dev" . saisie_to_sql($_SESSION['filtre_dev_num'],"num") . " AND ";
			}
			if (isset($_SESSION['filtre_dev_date']) && $_SESSION['filtre_dev_date'] != "")
			{
				$filtre .= "date" . saisie_to_sql($_SESSION['filtre_dev_date'],"date") . " AND ";
			}
			if (isset($_SESSION['filtre_dev_HT']) && $_SESSION['filtre_dev_HT'] != "")
			{
				$filtre .= "tot_htva" . saisie_to_sql($_SESSION['filtre_dev_HT'],"num") . " AND ";
			}
			if (isset($_SESSION['filtre_dev_TTC']) && $_SESSION['filtre_dev_TTC'] != "")
			{
				$filtre .= "tot_tva" . saisie_to_sql($_SESSION['filtre_dev_TTC'],"num") . " AND ";
			}
			
			$filtre_ok="";
			if (isset($_SESSION['filtre_dev_en_cours']) && $_SESSION['filtre_dev_en_cours'] == 1)
			{
				$filtre_ok = "resu='0' OR  ";
			}
			if (isset($_SESSION['filtre_dev_refuse']) && $_SESSION['filtre_dev_refuse'] == 1)
			{
				$filtre_ok .= "resu='per' OR  ";
			}
			if (isset($_SESSION['filtre_dev_accepte']) && $_SESSION['filtre_dev_accepte'] == 1)
			{
				$filtre_ok .= "resu='ok' OR ";
			}
			//si rien n'est coché
			if (  (!isset($_SESSION['filtre_dev_en_cours']) || $_SESSION['filtre_dev_en_cours'] == "")
						&&
					(!isset($_SESSION['filtre_dev_refuse']) || $_SESSION['filtre_dev_refuse'] == "")
						&&
					(!isset($_SESSION['filtre_dev_accepte']) || $_SESSION['filtre_dev_accepte'] == "")
					)
			{
				$filtre_ok = "resu='rien_du_tout' AND ";
			}

			$filtre .= "($filtre_ok ";

			$filtre = substr($filtre,0,strlen($filtre) -5); //retirer " AND " ou " OR  " de la fin du filtre
			$sql .= " $filtre )";


			
			

			// ----------------  FIN DE GESTION DES FILTRES  -----------------------------
			

			$tri_ordre=isset($_GET['tri_ordre'])?$_GET['tri_ordre']:"ASC"; //ordre de tri
			$tri_champ=isset($_GET['tri_champ'])?$_GET['tri_champ']:"date"; // champ de tri
			
			$sql .= " ORDER BY " . $tri_champ . " " . $tri_ordre;

			//echo $sql;

			$req = mysql_query($sql) or die("Erreur de Filtre !<br/><a href=devis_lister.php?annuler_filtre=oui>Cliquer ici</a></br/>");
			?>
		
			<center>

				<table class="boiteaction" width="80%">

					<tr>
						<td width="45%">
						<?php
							//$rqSql = "SELECT num_client, nom FROM " . $tblpref ."client WHERE actif != 'non'";

							$annee = date("Y");
							$mois = date("m");
							$jour = date("d");

						?>
							<form name="frm_dev" method="get" action="devis_edit.php" >

								<input type="hidden" name="nouv" value="ok" />
								<center>
								<table width="90%">
									<caption>
										<?php   echo $lang_devis_créer; ?>
									</caption>
									<tr>
										<td class="texte0"><?php echo "$lang_client"; ?></td>
										<td class="texte0">
											<?php
												inclure_combo_client("num_client",0,"frm_dev");
											?>
										</td>
									</tr>
									<tr>
										<td class="texte0" id="date"><?php aff_lang("Date") ?></td>
										<td class="texte0">
											<input type="text" name="date" value="<?php echo "$jour/$mois/$annee" ?>" />
											<a href="#" onclick=" window.open('include/pop.calendrier.php?frm=frm_dev&amp;ch=date','calendrier','width=415,height=160,scrollbars=0').focus();">
												<img src="image/petit_calendrier.gif" alt="calendrier" border="0"/>
											</a>
										</td>
									</tr>
									<tr>
										<td class="submit" colspan="2">
											<input type="button" value="<?php aff_lang("Créer le devis") ?>" onclick="verif_form()" />
										</td>
									</tr>
								</table>

								</center>
							</form>

						</td>
						<td width="5%" >&nbsp;</td>
						<td width="50%">
							<form name="frm_filtre" method="get" action="devis_lister.php" >
							<input type="hidden" name="filtrer"/>
								<table width="90%">
									<caption><?php aff_lang("Filtre") ?></caption>
									<tr>
										<td class="texte0" width="20%"><?php aff_lang("Devis n°") ?></td>
										<td class="texte0" width="30%">
											<input type="text" name="filtre_dev_num" value="<?php echo isset($_SESSION['filtre_dev_num'])?$_SESSION['filtre_dev_num']:"" ?>" />
										</td>
										<td class="submit" rowspan="6" width="20%">
											<br/><br/>
											<input type="submit" value="Filtrer !"/>
											<br/><br/>
											<a href="devis_lister.php?annuler_filtre=oui">
												<img border="0" src="image/delete.png" alt="<?php echo $lang_supprimer; ?>" />
											</a>
											<br/><br/>
											<?php bouton_aide("filtre") ?>
										</td>
									</tr>
									<tr>
										<td class="texte0"><?php aff_lang("Client") ?></td>
										<td class="texte0"><?php inclure_combo_client("filtre_dev_client",isset($_SESSION['filtre_dev_client'])?$_SESSION['filtre_dev_client']:"") ?> </td>
									</tr>
									<tr>
										<td class="texte0"><?php aff_lang("Date") ?></td>
										<td class="texte0">
											<input type="text" name="filtre_dev_date" value="<?php echo isset($_SESSION['filtre_dev_date'])?$_SESSION['filtre_dev_date']:"" ?>" />
										</td>
									</tr>
									<tr>
										<td class="texte0"><?php echo $lang_total_h_tva; ?></td>
										<td class="texte0">
											<input type="text" name="filtre_dev_HT" value="<?php echo isset($_SESSION['filtre_dev_HT'])?$_SESSION['filtre_dev_HT']:"" ?>" />
										</td>
									</tr>
									<tr>
										<td class="texte0"><?php echo $lang_total_ttc; ?></td>
										<td class="texte0">
											<input type="text" name="filtre_dev_TTC" value="<?php echo isset($_SESSION['filtre_dev_TTC'])?$_SESSION['filtre_dev_TTC']:"" ?>" />
										</td>
									</tr>
									<tr>
										<td class="texte0" colspan="2">
											<?php aff_lang("Devis en cours") ?> 
													<input type="checkbox" value="1" name="filtre_dev_en_cours" <?php if (isset($_SESSION['filtre_dev_en_cours']) && $_SESSION['filtre_dev_en_cours'] != "") { echo 'checked="checked"' ;} ?> /> &nbsp; 
											<?php aff_lang("Devis refusé") ?> 
													<input type="checkbox" value="1" name="filtre_dev_refuse" <?php if (isset($_SESSION['filtre_dev_refuse']) && $_SESSION['filtre_dev_refuse'] != "") { echo 'checked="checked"' ;} ?> /> &nbsp; 
											<?php aff_lang("Devis accepté") ?> 
													<input type="checkbox" value="1" name="filtre_dev_accepte" <?php if (isset($_SESSION['filtre_dev_accepte']) && $_SESSION['filtre_dev_accepte'] != "") { echo 'checked="checked"' ;} ?> /> &nbsp; 
										</td>
									</tr>
								</table>
							</form>

						</td>
					</tr>

				</table>

				<br/>

				<br/>

				<form name="frm_tri" action="devis_lister.php" method="get" >
					<input type="hidden" name="tri_champ" value="<?php echo $tri_champ ?>" />
					<input type="hidden" name="tri_ordre" value="<?php echo $tri_ordre ?>" />
				</form>

				<?php // Lister les devis exeistans ?>
				<table class="boiteaction">

					<tr>
						<th width="30"><a href="javascript:ecrit_ordre_et_post('frm_tri','num_dev')">N°</a> </th>
						<th width="180"><a href="javascript:ecrit_ordre_et_post('frm_tri','nom')"><?php echo $lang_client; ?></a> </th>
						<th width="90"><a href="javascript:ecrit_ordre_et_post('frm_tri','date')"><?php echo $lang_date; ?></a> </th>
						<th width="90"><a href="javascript:ecrit_ordre_et_post('frm_tri','tot_htva')"><?php aff_lang("Total HT") ?></a> </th>
						<th width="90"><a href="javascript:ecrit_ordre_et_post('frm_tri','tot_tva')"><?php aff_lang("Total TTC") ?></a> </th>
						<th colspan="5"><?php aff_lang("Action") ?> </th>
						<th colspan="2"><?php aff_lang("Action") ?> </th>
						<th colspan="2"><a href="javascript:ecrit_ordre_et_post('frm_tri','resu')"><?php aff_lang("Status") ?></a> &nbsp; <?php bouton_aide("devis_icones") ?></th>
					</tr>
					
					<?php
					$nombre = 1;
					$total_ht=0;
					$total_ttc=0;
					while($data = mysql_fetch_array($req))
					{
						$num_dev = $data['num_dev'];
						$total = $data['tot_htva'];
						$tva = $data['tot_tva'];
						$date = sql_to_date($data['date']);
						$nom = $data['nom'];
						$nom_html =urlencode($nom);
						$login = $data['login'];
						$mail = $data['mail'];
						$ttc = $total + $tva ; 
						$nom = htmlentities($data['nom'], ENT_QUOTES,'UTF-8');
						$nombre = $nombre +1;
						$resu= $data['resu'];
						if($nombre & 1)
						{
							$line="0";
						} else {
							$line="1";
						}
						$total_ht = $total_ht + $total;
						$total_ttc = $total_ttc + $ttc;

						?>
				  	<tr class="texte<?php echo $line ?>" onmouseover="this.className='highlight'" onmouseout="this.className='texte<?php echo $line ?>'">
					
						<td class="highlight"><?php echo $num_dev; ?></td>
						<td class="highlight"><?php echo $nom; ?></td>
						<td class="highlight"><?php echo $date; ?></td>
						<td class="highlight" style="text-align:right"><?php echo montant_financier ($total); ?>&nbsp;</td>
						<td class="highlight" style="text-align:right"><?php echo montant_financier ($ttc); ?>&nbsp;</td>
						<td class="highlight">
							<a href="devis_edit.php?num_devis=<?php echo $num_dev; ?>&amp;nom=<?php echo $nom_html; ?>"> 
								 <img src="image/edit.png" align="middle" border="0" alt="<?php echo $lang_editer; ?>" />
							</a>
						</td>
						<td class="highlight">
							<a href="devis_delete.php?num_devis=<?php echo $num_dev; ?>" onclick="return confirmDelete('<?php   echo"$lang_eff_dev $num_dev ?"; ?>')">
								<img src="image/delete.png" align="middle" border="0" alt="<?php echo $lang_supprimer; ?>" />
							</a>
						</td>
						<td class="highlight">
							<a href="fpdf/devis_pdf.php?num_dev=<?php echo $num_dev; ?>&amp;pdf_user=adm" target="_blank">
								<img src="image/imprimante.png" alt="<?php echo $lang_imprimer; ?>" align="middle" border="0" />
							</a>
						</td>
						<?php
						if ($mail != '' and $login != '') { ?>
					 		<td class="highlight">
								<a href="notifi_cli.php?type=devis&amp;mail=<?php   echo $mail; ?>">
							 		<img src="image/mail.gif" align="middle" alt="mail" border="0"/>
								</a>
							</td>
						<?php
						} else {
						?>
					 		<td class="highlight">&nbsp;</td>
						<?php
						}
						if($mail != '') { ?>
							<td class="highlight">
								<a href="fpdf/devis_pdf.php?num_dev=<?php   echo $num_dev; ?>&amp;nom=<?php   echo $nom; ?>&amp;action=mail&amp;pdf_user=adm" >
									<img src="image/pdf.gif" alt="<?php   echo $lang_imprimer; ?>" align="middle" border="0" />
								</a>
							</td>
						<?php
						} else { ?>
							<td class="highlight">&nbsp;</td>
						<?php
						}
						?> 
						<td class="highlight">
							<a href="devis_vers_commande.php?num_dev=<?php echo $num_dev; ?>" onclick="return confirmDelete('<?php echo "$lang_convert_dev $num_dev $lang_convert_dev2" ; ?>')" >
								 <img src="image/icon_lol.gif" alt="<?php aff_lang("Devis accepté par le client") ?>" title="<?php aff_lang("Devis accepté par le client") ?>" align="middle" border="0" />
							</a>
						</td>
						<td class="highlight">
							<a href="devis_non_commandes.php?num_dev=<?php echo $num_dev; ?>" onclick="return confirmDelete('<?php echo"$lang_dev_perd $num_dev $lang_dev_perd2 "; ?>')">
								<img src="image/icon_cry.gif" alt="<?php aff_lang("Devis refusé par le client") ?>" title="<?php aff_lang("Devis refusé par le client") ?>" align="middle" border="0" />
							</a>
						</td>
						<td class="highlight">
							<?php
								if ($resu=="0") { aff_lang("En cours"); }
								if ($resu=="ok") { aff_lang("Accepté"); }
								if ($resu=="per") { aff_lang("Refusé"); }
							?>
						</td>
					</tr>
					<?php
					}
					?>
					
					<tr>
						<td colspan="3">&nbsp;</td>
						<td colspan="2"> <hr/></td>
						<td colspan="8">&nbsp;</td>
					</tr>


					<tr>
						<td colspan="2">&nbsp;</td>
						<td style="text-align:center"><?php aff_lang("TOTAL")?></td>
						<td style="text-align:right"><?php echo montant_financier($total_ht) ?>&nbsp; </td>
						<td style="text-align:right"><?php echo montant_financier($total_ttc) ?>&nbsp; </td>
						<td colspan="8">&nbsp;</td>
					</tr>
				</table>
			</center>
			
		</td>
	</tr>
	<tr>
		<td>
			<?php
			require_once("include/bas.php");
			?>
		</td>
	</tr>

</table>
</body>
</html>

